package wp.ggt.com.wpview.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import wp.ggt.com.wpview.R;

public class MainActivity extends BaseActivity {
    private ImageView iv_photoView;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_photoView = (ImageView) findViewById(R.id.iv_photoView);
        boolean foreground = isForeground(this.getPackageName());
        Log.e("TAg", foreground + "---");
    }

    public void switchView(View view) {
        toActivity(SwitchActivyt.class);

    }

    public void toActivity(Class<? extends Activity> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    public void waterware(View view) {
        toActivity(WaterWareActivity.class);
    }

    public void Rxjava(View view) {
        toActivity(RxJavaActivity.class);
    }

    public void openPhoto(View view) {
        FunctionConfig config = new FunctionConfig.Builder()
                .setMutiSelectMaxSize(8)
                .build();
        GalleryFinal.openGalleryMuti(1, config, new GalleryFinal.OnHanlderResultCallback() {
            @Override
            public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
                ImageOptions options = new ImageOptions.Builder()
                        .setCrop(true)
                        .setAutoRotate(true)
                        .build();
                x.image().bind(iv_photoView, "file://" + resultList.get(0).getPhotoPath(), options);
            }

            @Override
            public void onHanlderFailure(int requestCode, String errorMsg) {

            }
        });
    }

    public void openCamera(View view) {
        GalleryFinal.openCamera(1, new GalleryFinal.OnHanlderResultCallback() {
            @Override
            public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
                ImageOptions options = new ImageOptions.Builder()
                        .setCrop(true)
                        .setAutoRotate(true)
                        .build();
                x.image().bind(iv_photoView, "file://" + resultList.get(0).getPhotoPath(), options);
            }

            @Override
            public void onHanlderFailure(int requestCode, String errorMsg) {

            }
        });
    }

    public void openViewGroup(View view) {
        toActivity(MyViewGroupActivity.class);
    }

    public boolean isForeground(String PackageName) {
        // Get the Activity Manager
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        // Get a list of running tasks, we are only interested in the last one,
        // the top most so we give a 1 as parameter so we only get the topmost.
        List<ActivityManager.RunningTaskInfo> task = manager.getRunningTasks(1);

        // Get the info we need for comparison.
        ComponentName componentInfo = task.get(0).topActivity;

        // Check if it matches our package name.
        if (componentInfo.getPackageName().equals(PackageName)) return true;

        // If not then our app is not on the foreground.
        return false;
    }
}
